-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- DBVersion: 1 
-- SCRIPT INICIAL. Creacion de las tablas.
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DBVersion]') AND type in (N'U'))
BEGIN
	CREATE TABLE [dbo].[DBVersion](
		[DBVersionNumber] [int] NOT NULL,
		[UpdateDate] [datetime] NOT NULL,
	 CONSTRAINT [PK_DBVersion] PRIMARY KEY CLUSTERED 
	(
		[DBVersionNumber] ASC
	)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
	) ON [PRIMARY]
	
	INSERT INTO [dbo].[DBVersion] ([DBVersionNumber], [UpdateDate]) VALUES (1, GETDATE())

	IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Tournaments]') AND type in (N'U'))
	BEGIN
		CREATE TABLE [dbo].[Tournaments](
			[TournamentId] [int] IDENTITY(1,1) NOT NULL,
			[Name] [nvarchar](50) NOT NULL,
			[IsActive] [bit] NOT NULL,
		 CONSTRAINT [PK_Tournaments] PRIMARY KEY CLUSTERED 
		(
			[TournamentId] ASC
		)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
		) ON [PRIMARY]
	END

	IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Journeys]') AND type in (N'U'))
	BEGIN
		CREATE TABLE [dbo].[Journeys](
			[JourneyId] [int] IDENTITY(1,1) NOT NULL,
			[TournamentId] [int] NOT NULL,
			[Number] [int] NOT NULL,
			[Date] [datetime] NOT NULL,
			[IsFinished] [bit] NOT NULL,
		 CONSTRAINT [PK_Journeys] PRIMARY KEY CLUSTERED 
		(
			[JourneyId] ASC
		)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
		) ON [PRIMARY]
	END

	IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Tournaments_Journeys]') AND parent_object_id = OBJECT_ID(N'[dbo].[Journeys]'))
	ALTER TABLE [dbo].[Journeys]  WITH CHECK ADD  CONSTRAINT [FK_Tournaments_Journeys] FOREIGN KEY([TournamentId])
	REFERENCES [dbo].[Tournaments] ([TournamentId])
END
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- DBVersion: 2
-- Modelo de torneos
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
IF NOT EXISTS (SELECT [DBVersionNumber] FROM [dbo].[DBVersion] WHERE [DBVersionNumber] = 2) BEGIN
	SET NUMERIC_ROUNDABORT OFF
	SET ANSI_PADDING ON

	SET ANSI_WARNINGS ON

	SET CONCAT_NULL_YIELDS_NULL ON

	SET ARITHABORT ON

	SET QUOTED_IDENTIFIER ON

	SET ANSI_NULLS ON

	IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors2')) DROP TABLE #tmpErrors2

	CREATE TABLE #tmpErrors2 (Error int)

	SET XACT_ABORT ON

	SET TRANSACTION ISOLATION LEVEL READ COMMITTED

	/*The column:  Date is being dropped from table:  [dbo].[Journeys].*/
	/*The column:  IsFinished is being dropped from table:  [dbo].[Journeys].*/
	IF EXISTS (select top 1 1 from [dbo].[Journeys])
		RAISERROR ('Rows detected, failing schema update due to possible data loss.', 16, 127) WITH NOWAIT

	BEGIN TRANSACTION

	PRINT N'Dropping foreign keys from [dbo].[Journeys]'

	ALTER TABLE [dbo].[Journeys] DROP
	CONSTRAINT [FK_Tournaments_Journeys]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Dropping constraints from [dbo].[Journeys]'

	ALTER TABLE [dbo].[Journeys] DROP CONSTRAINT [PK_Journeys]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Dropping constraints from [dbo].[Tournaments]'

	ALTER TABLE [dbo].[Tournaments] DROP CONSTRAINT [PK_Tournaments]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Rebuilding [dbo].[Tournaments]'

	SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
	SET XACT_ABORT ON

	BEGIN TRANSACTION
	CREATE TABLE [dbo].[tmp_ms_xx_Tournaments]
	(
	[TournamentId] [int] NOT NULL IDENTITY(1, 1),
	[TournamentType] [int] NOT NULL,
	[Name] [nvarchar] (50) COLLATE Modern_Spanish_CI_AS NOT NULL,
	[IsFinished] [bit] NOT NULL,
	[IsActive] [bit] NOT NULL,
	[FirstTeamId] [int] NULL,
	[SecondTeamId] [int] NULL,
	[ThirdTeamId] [int] NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Tournaments] ON
	INSERT INTO [dbo].[tmp_ms_xx_Tournaments]([TournamentId], [Name], [IsActive]) SELECT [TournamentId], [Name], [IsActive] FROM [dbo].[Tournaments]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Tournaments] OFF
	DROP TABLE [dbo].[Tournaments]
	EXEC sp_rename N'[dbo].[tmp_ms_xx_Tournaments]', N'Tournaments'
	COMMIT TRANSACTION

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	SET TRANSACTION ISOLATION LEVEL READ COMMITTED

	PRINT N'Creating primary key [PK_Tournaments] on [dbo].[Tournaments]'

	ALTER TABLE [dbo].[Tournaments] ADD CONSTRAINT [PK_Tournaments] PRIMARY KEY CLUSTERED  ([TournamentId]) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating index [UX_Tournaments_Name] on [dbo].[Tournaments]'

	CREATE UNIQUE NONCLUSTERED INDEX [UX_Tournaments_Name] ON [dbo].[Tournaments] ([Name]) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating [dbo].[TournamentsTeams]'

	CREATE TABLE [dbo].[TournamentsTeams]
	(
	[TournamentId] [int] NOT NULL,
	[TeamId] [int] NOT NULL
	) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating primary key [PK_TournamentsTeams] on [dbo].[TournamentsTeams]'

	ALTER TABLE [dbo].[TournamentsTeams] ADD CONSTRAINT [PK_TournamentsTeams] PRIMARY KEY CLUSTERED  ([TournamentId], [TeamId]) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating [dbo].[Teams]'

	CREATE TABLE [dbo].[Teams]
	(
	[TeamId] [int] NOT NULL IDENTITY(1, 1),
	[Name] [nvarchar] (50) COLLATE Modern_Spanish_CI_AS NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating primary key [PK_Teams] on [dbo].[Teams]'

	ALTER TABLE [dbo].[Teams] ADD CONSTRAINT [PK_Teams] PRIMARY KEY CLUSTERED  ([TeamId]) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating [dbo].[Matches]'

	CREATE TABLE [dbo].[Matches]
	(
	[MatchId] [int] NOT NULL IDENTITY(1, 1),
	[LocalTeamId] [int] NOT NULL,
	[VisitorTeamId] [int] NOT NULL,
	[IsPlayed] [bit] NOT NULL,
	[LocalGoals] [int] NOT NULL,
	[VisitorGoals] [int] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating primary key [PK_Matches] on [dbo].[Matches]'

	ALTER TABLE [dbo].[Matches] ADD CONSTRAINT [PK_Matches] PRIMARY KEY CLUSTERED  ([MatchId]) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating [dbo].[JourneysMatches]'

	CREATE TABLE [dbo].[JourneysMatches]
	(
	[JourneyId] [int] NOT NULL,
	[MatchId] [int] NOT NULL
	) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating primary key [PK_JourneysMatches] on [dbo].[JourneysMatches]'

	ALTER TABLE [dbo].[JourneysMatches] ADD CONSTRAINT [PK_JourneysMatches] PRIMARY KEY CLUSTERED  ([JourneyId], [MatchId]) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating index [UX_JourneysMatches_MatchId] on [dbo].[JourneysMatches]'

	CREATE UNIQUE NONCLUSTERED INDEX [UX_JourneysMatches_MatchId] ON [dbo].[JourneysMatches] ([MatchId]) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Rebuilding [dbo].[Journeys]'

	SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
	SET XACT_ABORT ON

	BEGIN TRANSACTION
	CREATE TABLE [dbo].[tmp_ms_xx_Journeys]
	(
	[JourneyId] [int] NOT NULL IDENTITY(1, 1),
	[TournamentId] [int] NOT NULL,
	[Number] [int] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Journeys] ON
	INSERT INTO [dbo].[tmp_ms_xx_Journeys]([JourneyId], [TournamentId], [Number]) SELECT [JourneyId], [TournamentId], [Number] FROM [dbo].[Journeys]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Journeys] OFF
	DROP TABLE [dbo].[Journeys]
	EXEC sp_rename N'[dbo].[tmp_ms_xx_Journeys]', N'Journeys'
	COMMIT TRANSACTION

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	SET TRANSACTION ISOLATION LEVEL READ COMMITTED

	PRINT N'Creating primary key [PK_Journeys] on [dbo].[Journeys]'

	ALTER TABLE [dbo].[Journeys] ADD CONSTRAINT [PK_Journeys] PRIMARY KEY CLUSTERED  ([JourneyId]) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating index [UX_Journeys_TournamentId_Number] on [dbo].[Journeys]'

	CREATE UNIQUE NONCLUSTERED INDEX [UX_Journeys_TournamentId_Number] ON [dbo].[Journeys] ([TournamentId], [Number]) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Adding foreign keys to [dbo].[JourneysMatches]'

	ALTER TABLE [dbo].[JourneysMatches] ADD
	CONSTRAINT [FK_JourneysMatches_Journeys] FOREIGN KEY ([JourneyId]) REFERENCES [dbo].[Journeys] ([JourneyId]) ON DELETE CASCADE,
	CONSTRAINT [FK_JourneysMatches_Matches] FOREIGN KEY ([MatchId]) REFERENCES [dbo].[Matches] ([MatchId])

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Adding foreign keys to [dbo].[Journeys]'

	ALTER TABLE [dbo].[Journeys] ADD
	CONSTRAINT [FK_Journeys_Tournaments] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId]) ON DELETE CASCADE

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Adding foreign keys to [dbo].[Matches]'

	ALTER TABLE [dbo].[Matches] ADD
	CONSTRAINT [FK_Matches_LocalTeams] FOREIGN KEY ([LocalTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_Matches_VisitorTeams] FOREIGN KEY ([VisitorTeamId]) REFERENCES [dbo].[Teams] ([TeamId])

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Adding foreign keys to [dbo].[TournamentsTeams]'

	ALTER TABLE [dbo].[TournamentsTeams] ADD
	CONSTRAINT [FK_Teams_TournamentsTeams] FOREIGN KEY ([TeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_Tournaments_TournamentsTeams] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId]) ON DELETE CASCADE

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Adding foreign keys to [dbo].[Tournaments]'

	ALTER TABLE [dbo].[Tournaments] ADD
	CONSTRAINT [FK_Tournaments_FirstTeams] FOREIGN KEY ([FirstTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_Tournaments_SecondTeams] FOREIGN KEY ([SecondTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_Tournaments_ThirdTeams] FOREIGN KEY ([ThirdTeamId]) REFERENCES [dbo].[Teams] ([TeamId])

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors2 (Error) SELECT 1 BEGIN TRANSACTION END

	IF EXISTS (SELECT * FROM #tmpErrors2) ROLLBACK TRANSACTION

	
	IF @@TRANCOUNT>0 BEGIN
		PRINT 'The database update succeeded.'
		COMMIT TRANSACTION
		INSERT INTO [dbo].[DBVersion] ([DBVersionNumber], [UpdateDate]) VALUES (2, GETDATE())
	END
	ELSE PRINT 'The database update failed.'
	

	DROP TABLE #tmpErrors2

END
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- DBVersion: 3 
-- Tabla Users
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
IF NOT EXISTS (SELECT [DBVersionNumber] FROM [dbo].[DBVersion] WHERE [DBVersionNumber] = 3)
BEGIN
	SET NUMERIC_ROUNDABORT OFF

	SET ANSI_PADDING ON

	SET ANSI_WARNINGS ON

	SET CONCAT_NULL_YIELDS_NULL ON

	SET ARITHABORT ON

	SET QUOTED_IDENTIFIER ON

	SET ANSI_NULLS ON

	IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors3')) DROP TABLE #tmpErrors3

	CREATE TABLE #tmpErrors3 (Error int)

	SET XACT_ABORT ON

	SET TRANSACTION ISOLATION LEVEL READ COMMITTED

	BEGIN TRANSACTION

	PRINT N'Creating [dbo].[Users]'

	CREATE TABLE [dbo].[Users]
	(
	[UserId] [int] NOT NULL IDENTITY(1, 1),
	[UserName] [nvarchar] (20) COLLATE Modern_Spanish_CI_AS NOT NULL,
	[Firstname] [nvarchar] (50) COLLATE Modern_Spanish_CI_AS NOT NULL,
	[Surname] [nvarchar] (50) COLLATE Modern_Spanish_CI_AS NOT NULL,
	[Md5Password] [nvarchar] (47) COLLATE Modern_Spanish_CI_AS NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors3 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating primary key [PK_Users] on [dbo].[Users]'

	ALTER TABLE [dbo].[Users] ADD CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED  ([UserId]) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors3 (Error) SELECT 1 BEGIN TRANSACTION END

	PRINT N'Creating index [UX_Users_UserName] on [dbo].[Users]'

	CREATE UNIQUE NONCLUSTERED INDEX [UX_Users_UserName] ON [dbo].[Users] ([UserName]) ON [PRIMARY]

	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION

	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors3 (Error) SELECT 1 BEGIN TRANSACTION END

	IF EXISTS (SELECT * FROM #tmpErrors3) ROLLBACK TRANSACTION

	IF @@TRANCOUNT>0 BEGIN
		PRINT 'The database update succeeded.'
		COMMIT TRANSACTION
		INSERT INTO [dbo].[DBVersion] ([DBVersionNumber], [UpdateDate]) VALUES (3, GETDATE())
	END
	ELSE PRINT 'The database update failed.'

	DROP TABLE #tmpErrors3

END
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- DBVersion: 4
-- Tabla Bets
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
IF NOT EXISTS (SELECT [DBVersionNumber] FROM [dbo].[DBVersion] WHERE [DBVersionNumber] = 4)
BEGIN

	SET NUMERIC_ROUNDABORT OFF
	SET ANSI_PADDING ON
	SET ANSI_WARNINGS ON
	SET CONCAT_NULL_YIELDS_NULL ON
	SET ARITHABORT ON
	SET QUOTED_IDENTIFIER ON
	SET ANSI_NULLS ON
	IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors4')) DROP TABLE #tmpErrors4
	CREATE TABLE #tmpErrors4 (Error int)
	SET XACT_ABORT ON
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	/*The column:  TournamentType is being dropped from table:  [dbo].[Tournaments].*/
	IF EXISTS (select top 1 1 from [dbo].[Tournaments])
		RAISERROR ('Rows detected, failing schema update due to possible data loss.', 16, 127) WITH NOWAIT
	BEGIN TRANSACTION
	PRINT N'Dropping foreign keys from [dbo].[Journeys]'
	ALTER TABLE [dbo].[Journeys] DROP
	CONSTRAINT [FK_Journeys_Tournaments]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[TournamentsTeams]'
	ALTER TABLE [dbo].[TournamentsTeams] DROP
	CONSTRAINT [FK_Tournaments_TournamentsTeams]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] DROP
	CONSTRAINT [FK_Tournaments_FirstTeams],
	CONSTRAINT [FK_Tournaments_SecondTeams],
	CONSTRAINT [FK_Tournaments_ThirdTeams]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping constraints from [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] DROP CONSTRAINT [PK_Tournaments]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping index [UX_Tournaments_Name] from [dbo].[Tournaments]'
	DROP INDEX [UX_Tournaments_Name] ON [dbo].[Tournaments]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating [dbo].[ThreeWaysBetsTargets]'
	CREATE TABLE [dbo].[ThreeWaysBetsTargets]
	(
	[ThreeWaysBetTargetId] [int] NOT NULL,
	[ExpectedResult] [int] NOT NULL,
	[MatchId] [int] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating primary key [PK_ThreeWaysBetsTargets] on [dbo].[ThreeWaysBetsTargets]'
	ALTER TABLE [dbo].[ThreeWaysBetsTargets] ADD CONSTRAINT [PK_ThreeWaysBetsTargets] PRIMARY KEY CLUSTERED  ([ThreeWaysBetTargetId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Rebuilding [dbo].[Tournaments]'
	SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
	SET XACT_ABORT ON
	BEGIN TRANSACTION
	CREATE TABLE [dbo].[tmp_ms_xx_Tournaments]
	(
	[TournamentId] [int] NOT NULL IDENTITY(1, 1),
	[Type] [int] NOT NULL,
	[Name] [nvarchar] (50) COLLATE Modern_Spanish_CI_AS NOT NULL,
	[IsFinished] [bit] NOT NULL,
	[IsActive] [bit] NOT NULL,
	[FirstTeamId] [int] NULL,
	[SecondTeamId] [int] NULL,
	[ThirdTeamId] [int] NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Tournaments] ON
	INSERT INTO [dbo].[tmp_ms_xx_Tournaments]([TournamentId], [Name], [IsFinished], [IsActive], [FirstTeamId], [SecondTeamId], [ThirdTeamId]) SELECT [TournamentId], [Name], [IsFinished], [IsActive], [FirstTeamId], [SecondTeamId], [ThirdTeamId] FROM [dbo].[Tournaments]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Tournaments] OFF
	DROP TABLE [dbo].[Tournaments]
	EXEC sp_rename N'[dbo].[tmp_ms_xx_Tournaments]', N'Tournaments'
	COMMIT TRANSACTION
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	PRINT N'Creating primary key [PK_Tournaments] on [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] ADD CONSTRAINT [PK_Tournaments] PRIMARY KEY CLUSTERED  ([TournamentId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating index [UX_Tournaments_Name] on [dbo].[Tournaments]'
	CREATE UNIQUE NONCLUSTERED INDEX [UX_Tournaments_Name] ON [dbo].[Tournaments] ([Name]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating [dbo].[Bets]'
	CREATE TABLE [dbo].[Bets]
	(
	[BetId] [int] NOT NULL IDENTITY(1, 1),
	[BettorUserId] [int] NOT NULL,
	[WageredMoney] [money] NOT NULL,
	[Odds] [float] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating primary key [PK_Bets] on [dbo].[Bets]'
	ALTER TABLE [dbo].[Bets] ADD CONSTRAINT [PK_Bets] PRIMARY KEY CLUSTERED  ([BetId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating [dbo].[TournamentPodiumBetsTargets]'
	CREATE TABLE [dbo].[TournamentPodiumBetsTargets]
	(
	[TournamentPodiumBetTargetId] [int] NOT NULL,
	[TournamentId] [int] NOT NULL,
	[FirstTeamId] [int] NOT NULL,
	[SecondTeamId] [int] NOT NULL,
	[ThirdTeamId] [int] NOT NULL
	) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating primary key [PK_TournamentPodiumBetsTargets] on [dbo].[TournamentPodiumBetsTargets]'
	ALTER TABLE [dbo].[TournamentPodiumBetsTargets] ADD CONSTRAINT [PK_TournamentPodiumBetsTargets] PRIMARY KEY CLUSTERED  ([TournamentPodiumBetTargetId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating [dbo].[BetsTypes]'
	CREATE TABLE [dbo].[BetsTypes]
	(
	[BetTypeId] [int] NOT NULL IDENTITY(1, 1),
	[Type] [int] NOT NULL,
	[CompetitionId] [int] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating primary key [PK_BetsTypes] on [dbo].[BetsTypes]'
	ALTER TABLE [dbo].[BetsTypes] ADD CONSTRAINT [PK_BetsTypes] PRIMARY KEY CLUSTERED  ([BetTypeId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating [dbo].[BetsTargets]'
	CREATE TABLE [dbo].[BetsTargets]
	(
	[BetTargetId] [int] NOT NULL,
	[Type] [int] NOT NULL,
	[BetTypeId] [int] NOT NULL,
	[Odds] [float] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating primary key [PK_BetsTargets] on [dbo].[BetsTargets]'
	ALTER TABLE [dbo].[BetsTargets] ADD CONSTRAINT [PK_BetsTargets] PRIMARY KEY CLUSTERED  ([BetTargetId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating [dbo].[BetsTargetsBets]'
	CREATE TABLE [dbo].[BetsTargetsBets]
	(
	[BetTargetId] [int] NOT NULL,
	[BetId] [int] NOT NULL
	) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating primary key [PK_BetsTargetsBets_1] on [dbo].[BetsTargetsBets]'
	ALTER TABLE [dbo].[BetsTargetsBets] ADD CONSTRAINT [PK_BetsTargetsBets_1] PRIMARY KEY CLUSTERED  ([BetTargetId], [BetId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating [dbo].[Accounts]'
	CREATE TABLE [dbo].[Accounts]
	(
	[AccountId] [int] NOT NULL IDENTITY(1, 1),
	[Balance] [float] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating primary key [PK_Accounts] on [dbo].[Accounts]'
	ALTER TABLE [dbo].[Accounts] ADD CONSTRAINT [PK_Accounts] PRIMARY KEY CLUSTERED  ([AccountId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating index [UX_Teams_Name] on [dbo].[Teams]'
	CREATE UNIQUE NONCLUSTERED INDEX [UX_Teams_Name] ON [dbo].[Teams] ([Name]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[BetsTargetsBets]'
	ALTER TABLE [dbo].[BetsTargetsBets] ADD
	CONSTRAINT [FK_BetsTargetsBets_Bets] FOREIGN KEY ([BetId]) REFERENCES [dbo].[Bets] ([BetId]),
	CONSTRAINT [FK_BetsTargetsBets_BetsTargets] FOREIGN KEY ([BetTargetId]) REFERENCES [dbo].[BetsTargets] ([BetTargetId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[Bets]'
	ALTER TABLE [dbo].[Bets] ADD
	CONSTRAINT [FK_Bets_Users] FOREIGN KEY ([BettorUserId]) REFERENCES [dbo].[Users] ([UserId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[ThreeWaysBetsTargets]'
	ALTER TABLE [dbo].[ThreeWaysBetsTargets] ADD
	CONSTRAINT [FK_ThreeWaysBetsTargets_BetsTargets] FOREIGN KEY ([ThreeWaysBetTargetId]) REFERENCES [dbo].[BetsTargets] ([BetTargetId]),
	CONSTRAINT [FK_ThreeWaysBetsTargets_Matches] FOREIGN KEY ([MatchId]) REFERENCES [dbo].[Matches] ([MatchId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[TournamentPodiumBetsTargets]'
	ALTER TABLE [dbo].[TournamentPodiumBetsTargets] ADD
	CONSTRAINT [FK_TournamentPodiumBetsTargets_BetsTargets] FOREIGN KEY ([TournamentPodiumBetTargetId]) REFERENCES [dbo].[BetsTargets] ([BetTargetId]),
	CONSTRAINT [FK_TournamentPodiumBetsTargets_Tournaments] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId]),
	CONSTRAINT [FK_TournamentPodiumBetsTargets_FirstTeams] FOREIGN KEY ([FirstTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_TournamentPodiumBetsTargets_SecondsTeams] FOREIGN KEY ([SecondTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_TournamentPodiumBetsTargets_ThirdTeams] FOREIGN KEY ([ThirdTeamId]) REFERENCES [dbo].[Teams] ([TeamId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[BetsTargets]'
	ALTER TABLE [dbo].[BetsTargets] ADD
	CONSTRAINT [FK_BetsTargets_BetsTypes] FOREIGN KEY ([BetTypeId]) REFERENCES [dbo].[BetsTypes] ([BetTypeId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[Journeys]'
	ALTER TABLE [dbo].[Journeys] ADD
	CONSTRAINT [FK_Journeys_Tournaments] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId]) ON DELETE CASCADE
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[TournamentsTeams]'
	ALTER TABLE [dbo].[TournamentsTeams] ADD
	CONSTRAINT [FK_Tournaments_TournamentsTeams] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId]) ON DELETE CASCADE
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] ADD
	CONSTRAINT [FK_Tournaments_FirstTeams] FOREIGN KEY ([FirstTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_Tournaments_SecondTeams] FOREIGN KEY ([SecondTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_Tournaments_ThirdTeams] FOREIGN KEY ([ThirdTeamId]) REFERENCES [dbo].[Teams] ([TeamId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors4 (Error) SELECT 1 BEGIN TRANSACTION END
	IF EXISTS (SELECT * FROM #tmpErrors4) ROLLBACK TRANSACTION
	IF @@TRANCOUNT>0 BEGIN
		PRINT 'The database update succeeded.'
		COMMIT TRANSACTION
		INSERT INTO [dbo].[DBVersion] ([DBVersionNumber], [UpdateDate]) VALUES (4, GETDATE())
	END
	ELSE PRINT 'The database update failed.'
	DROP TABLE #tmpErrors4
END
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- DBVersion: 5
-- Modificaciones en el modelo de apuestas y tabla de jugadores
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
IF NOT EXISTS (SELECT [DBVersionNumber] FROM [dbo].[DBVersion] WHERE [DBVersionNumber] = 5)
BEGIN
	SET NUMERIC_ROUNDABORT OFF
	SET ANSI_PADDING ON
	SET ANSI_WARNINGS ON
	SET CONCAT_NULL_YIELDS_NULL ON
	SET ARITHABORT ON
	SET QUOTED_IDENTIFIER ON
	SET ANSI_NULLS ON
	IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors5')) DROP TABLE #tmpErrors5
	CREATE TABLE #tmpErrors5 (Error int)
	SET XACT_ABORT ON
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	BEGIN TRANSACTION
	PRINT N'Dropping foreign keys from [dbo].[BetsTargetsBets]'
	ALTER TABLE [dbo].[BetsTargetsBets] DROP
	CONSTRAINT [FK_BetsTargetsBets_BetsTargets]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[ThreeWaysBetsTargets]'
	ALTER TABLE [dbo].[ThreeWaysBetsTargets] DROP
	CONSTRAINT [FK_ThreeWaysBetsTargets_BetsTargets]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[TournamentPodiumBetsTargets]'
	ALTER TABLE [dbo].[TournamentPodiumBetsTargets] DROP
	CONSTRAINT [FK_TournamentPodiumBetsTargets_BetsTargets],
	CONSTRAINT [FK_TournamentPodiumBetsTargets_Tournaments]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[BetsTargets]'
	ALTER TABLE [dbo].[BetsTargets] DROP
	CONSTRAINT [FK_BetsTargets_BetsTypes]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[Journeys]'
	ALTER TABLE [dbo].[Journeys] DROP
	CONSTRAINT [FK_Journeys_Tournaments]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[TournamentsTeams]'
	ALTER TABLE [dbo].[TournamentsTeams] DROP
	CONSTRAINT [FK_Tournaments_TournamentsTeams]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] DROP
	CONSTRAINT [FK_Tournaments_FirstTeams],
	CONSTRAINT [FK_Tournaments_SecondTeams],
	CONSTRAINT [FK_Tournaments_ThirdTeams]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping constraints from [dbo].[BetsTargets]'
	ALTER TABLE [dbo].[BetsTargets] DROP CONSTRAINT [PK_BetsTargets]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping constraints from [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] DROP CONSTRAINT [PK_Tournaments]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping index [UX_Tournaments_Name] from [dbo].[Tournaments]'
	DROP INDEX [UX_Tournaments_Name] ON [dbo].[Tournaments]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating [dbo].[Players]'
	CREATE TABLE [dbo].[Players]
	(
	[PlayerId] [int] NOT NULL IDENTITY(1, 1),
	[TeamId] [int] NOT NULL,
	[Name] [nvarchar] (50) COLLATE Modern_Spanish_CI_AS NOT NULL,
	[Role] [int] NOT NULL,
	[Speed] [int] NOT NULL,
	[Dribbling] [int] NOT NULL,
	[Strength] [int] NOT NULL,
	[Precision] [int] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating primary key [PK_Players] on [dbo].[Players]'
	ALTER TABLE [dbo].[Players] ADD CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED  ([PlayerId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Rebuilding [dbo].[BetsTargets]'
	SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
	SET XACT_ABORT ON
	BEGIN TRANSACTION
	CREATE TABLE [dbo].[tmp_ms_xx_BetsTargets]
	(
	[BetTargetId] [int] NOT NULL IDENTITY(1, 1),
	[Type] [int] NOT NULL,
	[BetTypeId] [int] NOT NULL,
	[Odds] [float] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_BetsTargets] ON
	INSERT INTO [dbo].[tmp_ms_xx_BetsTargets]([BetTargetId], [Type], [BetTypeId], [Odds]) SELECT [BetTargetId], [Type], [BetTypeId], [Odds] FROM [dbo].[BetsTargets]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_BetsTargets] OFF
	DROP TABLE [dbo].[BetsTargets]
	EXEC sp_rename N'[dbo].[tmp_ms_xx_BetsTargets]', N'BetsTargets'
	COMMIT TRANSACTION
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	PRINT N'Creating primary key [PK_BetsTargets] on [dbo].[BetsTargets]'
	ALTER TABLE [dbo].[BetsTargets] ADD CONSTRAINT [PK_BetsTargets] PRIMARY KEY CLUSTERED  ([BetTargetId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Rebuilding [dbo].[Tournaments]'
	SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
	SET XACT_ABORT ON
	BEGIN TRANSACTION
	CREATE TABLE [dbo].[tmp_ms_xx_Tournaments]
	(
	[TournamentId] [int] NOT NULL IDENTITY(1, 1),
	[Type] [int] NOT NULL,
	[Name] [nvarchar] (50) COLLATE Modern_Spanish_CI_AS NOT NULL,
	[IsFinished] [bit] NOT NULL,
	[IsActive] [bit] NOT NULL,
	[FirstTeamId] [int] NULL,
	[SecondTeamId] [int] NULL,
	[ThirdTeamId] [int] NULL,
	[CurrentJourneyNumber] [int] NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Tournaments] ON
	INSERT INTO [dbo].[tmp_ms_xx_Tournaments]([TournamentId], [Type], [Name], [IsFinished], [IsActive], [FirstTeamId], [SecondTeamId], [ThirdTeamId]) SELECT [TournamentId], [Type], [Name], [IsFinished], [IsActive], [FirstTeamId], [SecondTeamId], [ThirdTeamId] FROM [dbo].[Tournaments]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Tournaments] OFF
	DROP TABLE [dbo].[Tournaments]
	EXEC sp_rename N'[dbo].[tmp_ms_xx_Tournaments]', N'Tournaments'
	COMMIT TRANSACTION
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	PRINT N'Creating primary key [PK_Tournaments] on [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] ADD CONSTRAINT [PK_Tournaments] PRIMARY KEY CLUSTERED  ([TournamentId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating index [UX_Tournaments_Name] on [dbo].[Tournaments]'
	CREATE UNIQUE NONCLUSTERED INDEX [UX_Tournaments_Name] ON [dbo].[Tournaments] ([Name]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[BetsTargetsBets]'
	ALTER TABLE [dbo].[BetsTargetsBets] ADD
	CONSTRAINT [FK_BetsTargetsBets_BetsTargets] FOREIGN KEY ([BetTargetId]) REFERENCES [dbo].[BetsTargets] ([BetTargetId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[ThreeWaysBetsTargets]'
	ALTER TABLE [dbo].[ThreeWaysBetsTargets] ADD
	CONSTRAINT [FK_ThreeWaysBetsTargets_BetsTargets] FOREIGN KEY ([ThreeWaysBetTargetId]) REFERENCES [dbo].[BetsTargets] ([BetTargetId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[TournamentPodiumBetsTargets]'
	ALTER TABLE [dbo].[TournamentPodiumBetsTargets] ADD
	CONSTRAINT [FK_TournamentPodiumBetsTargets_BetsTargets] FOREIGN KEY ([TournamentPodiumBetTargetId]) REFERENCES [dbo].[BetsTargets] ([BetTargetId]),
	CONSTRAINT [FK_TournamentPodiumBetsTargets_Tournaments] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[BetsTargets]'
	ALTER TABLE [dbo].[BetsTargets] ADD
	CONSTRAINT [FK_BetsTargets_BetsTypes] FOREIGN KEY ([BetTypeId]) REFERENCES [dbo].[BetsTypes] ([BetTypeId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[Players]'
	ALTER TABLE [dbo].[Players] ADD
	CONSTRAINT [FK_Players_Teams] FOREIGN KEY ([TeamId]) REFERENCES [dbo].[Teams] ([TeamId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[Journeys]'
	ALTER TABLE [dbo].[Journeys] ADD
	CONSTRAINT [FK_Journeys_Tournaments] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId]) ON DELETE CASCADE
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[TournamentsTeams]'
	ALTER TABLE [dbo].[TournamentsTeams] ADD
	CONSTRAINT [FK_Tournaments_TournamentsTeams] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId]) ON DELETE CASCADE
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] ADD
	CONSTRAINT [FK_Tournaments_FirstTeams] FOREIGN KEY ([FirstTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_Tournaments_SecondTeams] FOREIGN KEY ([SecondTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_Tournaments_ThirdTeams] FOREIGN KEY ([ThirdTeamId]) REFERENCES [dbo].[Teams] ([TeamId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors5 (Error) SELECT 1 BEGIN TRANSACTION END
	IF EXISTS (SELECT * FROM #tmpErrors5) ROLLBACK TRANSACTION
	IF @@TRANCOUNT>0 BEGIN
		PRINT 'The database update succeeded.'
		COMMIT TRANSACTION
		INSERT INTO [dbo].[DBVersion] ([DBVersionNumber], [UpdateDate]) VALUES (5, GETDATE())
	END
		ELSE PRINT 'The database update failed.'
	DROP TABLE #tmpErrors5
END
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- DBVersion: 6
-- Modificaciones en tabla de jugadores
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
IF NOT EXISTS (SELECT [DBVersionNumber] FROM [dbo].[DBVersion] WHERE [DBVersionNumber] = 6)
BEGIN
	SET NUMERIC_ROUNDABORT OFF
	SET ANSI_PADDING ON
	SET ANSI_WARNINGS ON
	SET CONCAT_NULL_YIELDS_NULL ON
	SET ARITHABORT ON
	SET QUOTED_IDENTIFIER ON
	SET ANSI_NULLS ON
	IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors6')) DROP TABLE #tmpErrors6
	CREATE TABLE #tmpErrors6 (Error int)
	SET XACT_ABORT ON
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	/*The column:  Name is being dropped from table:  [dbo].[Players].*/
	IF EXISTS (select top 1 1 from [dbo].[Players])
		RAISERROR ('Rows detected, failing schema update due to possible data loss.', 16, 127) WITH NOWAIT
	BEGIN TRANSACTION
	PRINT N'Dropping foreign keys from [dbo].[Players]'
	ALTER TABLE [dbo].[Players] DROP
	CONSTRAINT [FK_Players_Teams]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors6 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping constraints from [dbo].[Players]'
	ALTER TABLE [dbo].[Players] DROP CONSTRAINT [PK_Players]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors6 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Rebuilding [dbo].[Players]'
	SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
	SET XACT_ABORT ON
	BEGIN TRANSACTION
	CREATE TABLE [dbo].[tmp_ms_xx_Players]
	(
	[PlayerId] [int] NOT NULL IDENTITY(1, 1),
	[TeamId] [int] NOT NULL,
	[FirstName] [nvarchar] (50) COLLATE Modern_Spanish_CI_AS NOT NULL,
	[Surname] [nvarchar] (50) COLLATE Modern_Spanish_CI_AS NOT NULL,
	[Role] [int] NOT NULL,
	[Speed] [int] NOT NULL,
	[Dribbling] [int] NOT NULL,
	[Strength] [int] NOT NULL,
	[Precision] [int] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Players] ON
	INSERT INTO [dbo].[tmp_ms_xx_Players]([PlayerId], [TeamId], [Role], [Speed], [Dribbling], [Strength], [Precision]) SELECT [PlayerId], [TeamId], [Role], [Speed], [Dribbling], [Strength], [Precision] FROM [dbo].[Players]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Players] OFF
	DROP TABLE [dbo].[Players]
	EXEC sp_rename N'[dbo].[tmp_ms_xx_Players]', N'Players'
	COMMIT TRANSACTION
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors6 (Error) SELECT 1 BEGIN TRANSACTION END
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	PRINT N'Creating primary key [PK_Players] on [dbo].[Players]'
	ALTER TABLE [dbo].[Players] ADD CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED  ([PlayerId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors6 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating index [UX_Players_TeamId_Name] on [dbo].[Players]'
	CREATE UNIQUE NONCLUSTERED INDEX [UX_Players_TeamId_Name] ON [dbo].[Players] ([TeamId], [Surname]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors6 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[Players]'
	ALTER TABLE [dbo].[Players] ADD
	CONSTRAINT [FK_Players_Teams] FOREIGN KEY ([TeamId]) REFERENCES [dbo].[Teams] ([TeamId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors6 (Error) SELECT 1 BEGIN TRANSACTION END
	IF EXISTS (SELECT * FROM #tmpErrors6) ROLLBACK TRANSACTION
	IF @@TRANCOUNT>0 BEGIN
		PRINT 'The database update succeeded.'
		COMMIT TRANSACTION
		INSERT INTO [dbo].[DBVersion] ([DBVersionNumber], [UpdateDate]) VALUES (6, GETDATE())
	END
		ELSE PRINT 'The database update failed.'
	DROP TABLE #tmpErrors6
END
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- DBVersion: 7
-- Modificaciones en apuestas por torneos
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
IF NOT EXISTS (SELECT [DBVersionNumber] FROM [dbo].[DBVersion] WHERE [DBVersionNumber] = 7)
BEGIN
	SET NUMERIC_ROUNDABORT OFF
	SET ANSI_PADDING ON
	SET ANSI_WARNINGS ON
	SET CONCAT_NULL_YIELDS_NULL ON
	SET ARITHABORT ON
	SET QUOTED_IDENTIFIER ON
	SET ANSI_NULLS ON
	IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors7')) DROP TABLE #tmpErrors7
	CREATE TABLE #tmpErrors7 (Error int)
	SET XACT_ABORT ON
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	BEGIN TRANSACTION
	PRINT N'Dropping foreign keys from [dbo].[TournamentPodiumBetsTargets]'
	ALTER TABLE [dbo].[TournamentPodiumBetsTargets] DROP
	CONSTRAINT [FK_TournamentPodiumBetsTargets_BetsTargets],
	CONSTRAINT [FK_TournamentPodiumBetsTargets_Tournaments],
	CONSTRAINT [FK_TournamentPodiumBetsTargets_FirstTeams],
	CONSTRAINT [FK_TournamentPodiumBetsTargets_SecondsTeams],
	CONSTRAINT [FK_TournamentPodiumBetsTargets_ThirdTeams]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors7 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping constraints from [dbo].[TournamentPodiumBetsTargets]'
	ALTER TABLE [dbo].[TournamentPodiumBetsTargets] DROP CONSTRAINT [PK_TournamentPodiumBetsTargets]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors7 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Rebuilding [dbo].[TournamentPodiumBetsTargets]'
	SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
	SET XACT_ABORT ON
	BEGIN TRANSACTION
	CREATE TABLE [dbo].[tmp_ms_xx_TournamentPodiumBetsTargets]
	(
	[TournamentPodiumBetTargetId] [int] NOT NULL,
	[TournamentId] [int] NOT NULL,
	[FirstTeamId] [int] NOT NULL,
	[SecondTeamId] [int] NOT NULL,
	[ThirdTeamId] [int] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	INSERT INTO [dbo].[tmp_ms_xx_TournamentPodiumBetsTargets]([TournamentPodiumBetTargetId], [TournamentId], [FirstTeamId], [SecondTeamId], [ThirdTeamId]) SELECT [TournamentPodiumBetTargetId], [TournamentId], [FirstTeamId], [SecondTeamId], [ThirdTeamId] FROM [dbo].[TournamentPodiumBetsTargets]
	DROP TABLE [dbo].[TournamentPodiumBetsTargets]
	EXEC sp_rename N'[dbo].[tmp_ms_xx_TournamentPodiumBetsTargets]', N'TournamentPodiumBetsTargets'
	COMMIT TRANSACTION
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors7 (Error) SELECT 1 BEGIN TRANSACTION END
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	PRINT N'Creating primary key [PK_TournamentPodiumBetsTargets] on [dbo].[TournamentPodiumBetsTargets]'
	ALTER TABLE [dbo].[TournamentPodiumBetsTargets] ADD CONSTRAINT [PK_TournamentPodiumBetsTargets] PRIMARY KEY CLUSTERED  ([TournamentPodiumBetTargetId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors7 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[TournamentPodiumBetsTargets]'
	ALTER TABLE [dbo].[TournamentPodiumBetsTargets] ADD
	CONSTRAINT [FK_TournamentPodiumBetsTargets_BetsTargets] FOREIGN KEY ([TournamentPodiumBetTargetId]) REFERENCES [dbo].[BetsTargets] ([BetTargetId]),
	CONSTRAINT [FK_TournamentPodiumBetsTargets_Tournaments] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId]),
	CONSTRAINT [FK_TournamentPodiumBetsTargets_FirstTeams] FOREIGN KEY ([FirstTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_TournamentPodiumBetsTargets_SecondsTeams] FOREIGN KEY ([SecondTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_TournamentPodiumBetsTargets_ThirdTeams] FOREIGN KEY ([ThirdTeamId]) REFERENCES [dbo].[Teams] ([TeamId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors7 (Error) SELECT 1 BEGIN TRANSACTION END
	IF EXISTS (SELECT * FROM #tmpErrors7) ROLLBACK TRANSACTION
	IF @@TRANCOUNT>0 BEGIN
		PRINT 'The database update succeeded.'
		COMMIT TRANSACTION
		INSERT INTO [dbo].[DBVersion] ([DBVersionNumber], [UpdateDate]) VALUES (7, GETDATE())
	END
		ELSE PRINT 'The database update failed.'
	DROP TABLE #tmpErrors7
END
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- DBVersion: 8
-- Modificaciones en apuestas por jugadores
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
IF NOT EXISTS (SELECT [DBVersionNumber] FROM [dbo].[DBVersion] WHERE [DBVersionNumber] = 8)
BEGIN
	SET NUMERIC_ROUNDABORT OFF
	SET ANSI_PADDING ON
	SET ANSI_WARNINGS ON
	SET CONCAT_NULL_YIELDS_NULL ON
	SET ARITHABORT ON
	SET QUOTED_IDENTIFIER ON
	SET ANSI_NULLS ON
	IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors8')) DROP TABLE #tmpErrors8
	CREATE TABLE #tmpErrors8 (Error int)
	SET XACT_ABORT ON
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	BEGIN TRANSACTION
	PRINT N'Dropping foreign keys from [dbo].[Players]'
	ALTER TABLE [dbo].[Players] DROP
	CONSTRAINT [FK_Players_Teams]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors8 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping index [UX_Players_TeamId_Name] from [dbo].[Players]'
	DROP INDEX [UX_Players_TeamId_Name] ON [dbo].[Players]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors8 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating index [UX_Players_TeamId_TeamId_FirstName_Surname] on [dbo].[Players]'
	CREATE UNIQUE NONCLUSTERED INDEX [UX_Players_TeamId_TeamId_FirstName_Surname] ON [dbo].[Players] ([TeamId], [FirstName], [Surname]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors8 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[Players]'
	ALTER TABLE [dbo].[Players] ADD
	CONSTRAINT [FK_Players_Teams] FOREIGN KEY ([TeamId]) REFERENCES [dbo].[Teams] ([TeamId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors8 (Error) SELECT 1 BEGIN TRANSACTION END
	IF EXISTS (SELECT * FROM #tmpErrors8) ROLLBACK TRANSACTION
	IF @@TRANCOUNT>0 BEGIN
		PRINT 'The database update succeeded.'
		COMMIT TRANSACTION
		INSERT INTO [dbo].[DBVersion] ([DBVersionNumber], [UpdateDate]) VALUES (8, GETDATE())
	END
		ELSE PRINT 'The database update failed.'
	DROP TABLE #tmpErrors8
END
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- DBVersion: 9
-- Cambio Type por Kind
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
IF NOT EXISTS (SELECT [DBVersionNumber] FROM [dbo].[DBVersion] WHERE [DBVersionNumber] = 9)
BEGIN
	SET NUMERIC_ROUNDABORT OFF
	SET ANSI_PADDING ON
	SET ANSI_WARNINGS ON
	SET CONCAT_NULL_YIELDS_NULL ON
	SET ARITHABORT ON
	SET QUOTED_IDENTIFIER ON
	SET ANSI_NULLS ON
	IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors9')) DROP TABLE #tmpErrors9
	CREATE TABLE #tmpErrors9 (Error int)
	SET XACT_ABORT ON
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	/*The column:  Type is being dropped from table:  [dbo].[BetsTargets].*/
	IF EXISTS (select top 1 1 from [dbo].[BetsTargets])
		RAISERROR ('Rows detected, failing schema update due to possible data loss.', 16, 127) WITH NOWAIT
	/*The column:  Type is being dropped from table:  [dbo].[BetsTypes].*/
	IF EXISTS (select top 1 1 from [dbo].[BetsTypes])
		RAISERROR ('Rows detected, failing schema update due to possible data loss.', 16, 127) WITH NOWAIT
	/*The column:  Type is being dropped from table:  [dbo].[Tournaments].*/
	IF EXISTS (select top 1 1 from [dbo].[Tournaments])
		RAISERROR ('Rows detected, failing schema update due to possible data loss.', 16, 127) WITH NOWAIT
	BEGIN TRANSACTION
	PRINT N'Dropping foreign keys from [dbo].[BetsTargetsBets]'
	ALTER TABLE [dbo].[BetsTargetsBets] DROP
	CONSTRAINT [FK_BetsTargetsBets_BetsTargets]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[ThreeWaysBetsTargets]'
	ALTER TABLE [dbo].[ThreeWaysBetsTargets] DROP
	CONSTRAINT [FK_ThreeWaysBetsTargets_BetsTargets]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[TournamentPodiumBetsTargets]'
	ALTER TABLE [dbo].[TournamentPodiumBetsTargets] DROP
	CONSTRAINT [FK_TournamentPodiumBetsTargets_BetsTargets],
	CONSTRAINT [FK_TournamentPodiumBetsTargets_Tournaments]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[BetsTargets]'
	ALTER TABLE [dbo].[BetsTargets] DROP
	CONSTRAINT [FK_BetsTargets_BetsTypes]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[Journeys]'
	ALTER TABLE [dbo].[Journeys] DROP
	CONSTRAINT [FK_Journeys_Tournaments]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[TournamentsTeams]'
	ALTER TABLE [dbo].[TournamentsTeams] DROP
	CONSTRAINT [FK_Tournaments_TournamentsTeams]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping foreign keys from [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] DROP
	CONSTRAINT [FK_Tournaments_FirstTeams],
	CONSTRAINT [FK_Tournaments_SecondTeams],
	CONSTRAINT [FK_Tournaments_ThirdTeams]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping constraints from [dbo].[BetsTargets]'
	ALTER TABLE [dbo].[BetsTargets] DROP CONSTRAINT [PK_BetsTargets]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping constraints from [dbo].[BetsTypes]'
	ALTER TABLE [dbo].[BetsTypes] DROP CONSTRAINT [PK_BetsTypes]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping constraints from [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] DROP CONSTRAINT [PK_Tournaments]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping index [UX_Tournaments_Name] from [dbo].[Tournaments]'
	DROP INDEX [UX_Tournaments_Name] ON [dbo].[Tournaments]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Rebuilding [dbo].[BetsTargets]'
	SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
	SET XACT_ABORT ON
	BEGIN TRANSACTION
	CREATE TABLE [dbo].[tmp_ms_xx_BetsTargets]
	(
	[BetTargetId] [int] NOT NULL IDENTITY(1, 1),
	[Kind] [int] NOT NULL,
	[BetTypeId] [int] NOT NULL,
	[Odds] [float] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_BetsTargets] ON
	INSERT INTO [dbo].[tmp_ms_xx_BetsTargets]([BetTargetId], [BetTypeId], [Odds]) SELECT [BetTargetId], [BetTypeId], [Odds] FROM [dbo].[BetsTargets]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_BetsTargets] OFF
	DROP TABLE [dbo].[BetsTargets]
	EXEC sp_rename N'[dbo].[tmp_ms_xx_BetsTargets]', N'BetsTargets'
	COMMIT TRANSACTION
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	PRINT N'Creating primary key [PK_BetsTargets] on [dbo].[BetsTargets]'
	ALTER TABLE [dbo].[BetsTargets] ADD CONSTRAINT [PK_BetsTargets] PRIMARY KEY CLUSTERED  ([BetTargetId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Rebuilding [dbo].[Tournaments]'
	SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
	SET XACT_ABORT ON
	BEGIN TRANSACTION
	CREATE TABLE [dbo].[tmp_ms_xx_Tournaments]
	(
	[TournamentId] [int] NOT NULL IDENTITY(1, 1),
	[Kind] [int] NOT NULL,
	[Name] [nvarchar] (50) COLLATE Modern_Spanish_CI_AS NOT NULL,
	[IsFinished] [bit] NOT NULL,
	[IsActive] [bit] NOT NULL,
	[FirstTeamId] [int] NULL,
	[SecondTeamId] [int] NULL,
	[ThirdTeamId] [int] NULL,
	[CurrentJourneyNumber] [int] NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Tournaments] ON
	INSERT INTO [dbo].[tmp_ms_xx_Tournaments]([TournamentId], [Name], [IsFinished], [IsActive], [FirstTeamId], [SecondTeamId], [ThirdTeamId], [CurrentJourneyNumber]) SELECT [TournamentId], [Name], [IsFinished], [IsActive], [FirstTeamId], [SecondTeamId], [ThirdTeamId], [CurrentJourneyNumber] FROM [dbo].[Tournaments]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_Tournaments] OFF
	DROP TABLE [dbo].[Tournaments]
	EXEC sp_rename N'[dbo].[tmp_ms_xx_Tournaments]', N'Tournaments'
	COMMIT TRANSACTION
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	PRINT N'Creating primary key [PK_Tournaments] on [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] ADD CONSTRAINT [PK_Tournaments] PRIMARY KEY CLUSTERED  ([TournamentId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating index [UX_Tournaments_Name] on [dbo].[Tournaments]'
	CREATE UNIQUE NONCLUSTERED INDEX [UX_Tournaments_Name] ON [dbo].[Tournaments] ([Name]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Rebuilding [dbo].[BetsTypes]'
	SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
	SET XACT_ABORT ON
	BEGIN TRANSACTION
	CREATE TABLE [dbo].[tmp_ms_xx_BetsTypes]
	(
	[BetTypeId] [int] NOT NULL IDENTITY(1, 1),
	[Kind] [int] NOT NULL,
	[CompetitionId] [int] NOT NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_BetsTypes] ON
	INSERT INTO [dbo].[tmp_ms_xx_BetsTypes]([BetTypeId], [CompetitionId]) SELECT [BetTypeId], [CompetitionId] FROM [dbo].[BetsTypes]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_BetsTypes] OFF
	DROP TABLE [dbo].[BetsTypes]
	EXEC sp_rename N'[dbo].[tmp_ms_xx_BetsTypes]', N'BetsTypes'
	COMMIT TRANSACTION
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	PRINT N'Creating primary key [PK_BetsTypes] on [dbo].[BetsTypes]'
	ALTER TABLE [dbo].[BetsTypes] ADD CONSTRAINT [PK_BetsTypes] PRIMARY KEY CLUSTERED  ([BetTypeId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[BetsTargetsBets]'
	ALTER TABLE [dbo].[BetsTargetsBets] ADD
	CONSTRAINT [FK_BetsTargetsBets_BetsTargets] FOREIGN KEY ([BetTargetId]) REFERENCES [dbo].[BetsTargets] ([BetTargetId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[ThreeWaysBetsTargets]'
	ALTER TABLE [dbo].[ThreeWaysBetsTargets] ADD
	CONSTRAINT [FK_ThreeWaysBetsTargets_BetsTargets] FOREIGN KEY ([ThreeWaysBetTargetId]) REFERENCES [dbo].[BetsTargets] ([BetTargetId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[TournamentPodiumBetsTargets]'
	ALTER TABLE [dbo].[TournamentPodiumBetsTargets] ADD
	CONSTRAINT [FK_TournamentPodiumBetsTargets_BetsTargets] FOREIGN KEY ([TournamentPodiumBetTargetId]) REFERENCES [dbo].[BetsTargets] ([BetTargetId]),
	CONSTRAINT [FK_TournamentPodiumBetsTargets_Tournaments] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[BetsTargets]'
	ALTER TABLE [dbo].[BetsTargets] ADD
	CONSTRAINT [FK_BetsTargets_BetsTypes] FOREIGN KEY ([BetTypeId]) REFERENCES [dbo].[BetsTypes] ([BetTypeId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[Journeys]'
	ALTER TABLE [dbo].[Journeys] ADD
	CONSTRAINT [FK_Journeys_Tournaments] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId]) ON DELETE CASCADE
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[TournamentsTeams]'
	ALTER TABLE [dbo].[TournamentsTeams] ADD
	CONSTRAINT [FK_Tournaments_TournamentsTeams] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId]) ON DELETE CASCADE
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] ADD
	CONSTRAINT [FK_Tournaments_FirstTeams] FOREIGN KEY ([FirstTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_Tournaments_SecondTeams] FOREIGN KEY ([SecondTeamId]) REFERENCES [dbo].[Teams] ([TeamId]),
	CONSTRAINT [FK_Tournaments_ThirdTeams] FOREIGN KEY ([ThirdTeamId]) REFERENCES [dbo].[Teams] ([TeamId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors9 (Error) SELECT 1 BEGIN TRANSACTION END
	IF EXISTS (SELECT * FROM #tmpErrors9) ROLLBACK TRANSACTION
	IF @@TRANCOUNT>0 BEGIN
		PRINT 'The database update succeeded.'
		COMMIT TRANSACTION
		INSERT INTO [dbo].[DBVersion] ([DBVersionNumber], [UpdateDate]) VALUES (9, GETDATE())
	END
		ELSE PRINT 'The database update failed.'
	DROP TABLE #tmpErrors9
END
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- DBVersion: 10
-- tabla de posciones de los torneos
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
IF NOT EXISTS (SELECT [DBVersionNumber] FROM [dbo].[DBVersion] WHERE [DBVersionNumber] = 10)
BEGIN
	SET NUMERIC_ROUNDABORT OFF
	SET ANSI_PADDING ON
	SET ANSI_WARNINGS ON
	SET CONCAT_NULL_YIELDS_NULL ON
	SET ARITHABORT ON
	SET QUOTED_IDENTIFIER ON
	SET ANSI_NULLS ON
	IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors10')) DROP TABLE #tmpErrors10
	CREATE TABLE #tmpErrors10 (Error int)
	SET XACT_ABORT ON
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	BEGIN TRANSACTION
	PRINT N'Altering [dbo].[Tournaments]'
	ALTER TABLE [dbo].[Tournaments] ALTER COLUMN [CurrentJourneyNumber] [int] NOT NULL
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors10 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating [dbo].[Standings]'
	CREATE TABLE [dbo].[Standings]
	(
	[StandingsId] [int] NOT NULL IDENTITY(1, 1),
	[TournamentId] [int] NOT NULL,
	[TeamId] [int] NOT NULL,
	[Points] [int] NOT NULL,
	[GoalsFor] [int] NOT NULL,
	[GoalsAgainst] [int] NOT NULL,
	[GoalsDifference] [int] NOT NULL,
	[PlayedMatches] [int] NOT NULL,
	[WonMatches] [int] NOT NULL,
	[DrawMatches] [int] NOT NULL,
	[LostMatches] [int] NOT NULL
	) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors10 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating primary key [PK_Standings] on [dbo].[Standings]'
	ALTER TABLE [dbo].[Standings] ADD CONSTRAINT [PK_Standings] PRIMARY KEY CLUSTERED  ([StandingsId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors10 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Creating index [UX_Standings_TournamentId_TeamId] on [dbo].[Standings]'
	CREATE UNIQUE NONCLUSTERED INDEX [UX_Standings_TournamentId_TeamId] ON [dbo].[Standings] ([TournamentId], [TeamId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors10 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[Standings]'
	ALTER TABLE [dbo].[Standings] ADD
	CONSTRAINT [FK_Standings_Tournaments] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId]),
	CONSTRAINT [FK_Standings_Teams] FOREIGN KEY ([TeamId]) REFERENCES [dbo].[Teams] ([TeamId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors10 (Error) SELECT 1 BEGIN TRANSACTION END
	IF EXISTS (SELECT * FROM #tmpErrors10) ROLLBACK TRANSACTION
	IF @@TRANCOUNT>0 BEGIN
		PRINT 'The database update succeeded.'
		COMMIT TRANSACTION
		INSERT INTO [dbo].[DBVersion] ([DBVersionNumber], [UpdateDate]) VALUES (10, GETDATE())
	END
		ELSE PRINT 'The database update failed.'
	DROP TABLE #tmpErrors10
END
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- DBVersion: 11
-- cambios en bettype
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
-- *****************************************************************************************************************
IF NOT EXISTS (SELECT [DBVersionNumber] FROM [dbo].[DBVersion] WHERE [DBVersionNumber] = 11)
BEGIN
	SET NUMERIC_ROUNDABORT OFF
	SET ANSI_PADDING ON
	SET ANSI_WARNINGS ON
	SET CONCAT_NULL_YIELDS_NULL ON
	SET ARITHABORT ON
	SET QUOTED_IDENTIFIER ON
	SET ANSI_NULLS ON
	IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors11')) DROP TABLE #tmpErrors11
	CREATE TABLE #tmpErrors11 (Error int)
	SET XACT_ABORT ON
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	/*The column:  CompetitionId is being dropped from table:  [dbo].[BetsTypes].*/
	IF EXISTS (select top 1 1 from [dbo].[BetsTypes])
		RAISERROR ('Rows detected, failing schema update due to possible data loss.', 16, 127) WITH NOWAIT
	BEGIN TRANSACTION
	PRINT N'Dropping foreign keys from [dbo].[BetsTargets]'
	ALTER TABLE [dbo].[BetsTargets] DROP
	CONSTRAINT [FK_BetsTargets_BetsTypes]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors11 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Dropping constraints from [dbo].[BetsTypes]'
	ALTER TABLE [dbo].[BetsTypes] DROP CONSTRAINT [PK_BetsTypes]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors11 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Rebuilding [dbo].[BetsTypes]'
	SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
	SET XACT_ABORT ON
	BEGIN TRANSACTION
	CREATE TABLE [dbo].[tmp_ms_xx_BetsTypes]
	(
	[BetTypeId] [int] NOT NULL IDENTITY(1, 1),
	[Kind] [int] NOT NULL,
	[TournamentId] [int] NULL,
	[MatchId] [int] NULL,
	[Timestamp] [timestamp] NOT NULL
	) ON [PRIMARY]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_BetsTypes] ON
	INSERT INTO [dbo].[tmp_ms_xx_BetsTypes]([BetTypeId], [Kind]) SELECT [BetTypeId], [Kind] FROM [dbo].[BetsTypes]
	SET IDENTITY_INSERT [dbo].[tmp_ms_xx_BetsTypes] OFF
	DROP TABLE [dbo].[BetsTypes]
	EXEC sp_rename N'[dbo].[tmp_ms_xx_BetsTypes]', N'BetsTypes'
	COMMIT TRANSACTION
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors11 (Error) SELECT 1 BEGIN TRANSACTION END
	SET TRANSACTION ISOLATION LEVEL READ COMMITTED
	PRINT N'Creating primary key [PK_BetsTypes] on [dbo].[BetsTypes]'
	ALTER TABLE [dbo].[BetsTypes] ADD CONSTRAINT [PK_BetsTypes] PRIMARY KEY CLUSTERED  ([BetTypeId]) ON [PRIMARY]
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors11 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[BetsTargets]'
	ALTER TABLE [dbo].[BetsTargets] ADD
	CONSTRAINT [FK_BetsTargets_BetsTypes] FOREIGN KEY ([BetTypeId]) REFERENCES [dbo].[BetsTypes] ([BetTypeId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors11 (Error) SELECT 1 BEGIN TRANSACTION END
	PRINT N'Adding foreign keys to [dbo].[BetsTypes]'
	ALTER TABLE [dbo].[BetsTypes] ADD
	CONSTRAINT [FK_BetsTypes_Tournaments] FOREIGN KEY ([TournamentId]) REFERENCES [dbo].[Tournaments] ([TournamentId]),
	CONSTRAINT [FK_BetsTypes_Matches] FOREIGN KEY ([MatchId]) REFERENCES [dbo].[Matches] ([MatchId])
	IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
	IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors11 (Error) SELECT 1 BEGIN TRANSACTION END
	IF EXISTS (SELECT * FROM #tmpErrors11) ROLLBACK TRANSACTION
	IF @@TRANCOUNT>0 BEGIN
		PRINT 'The database update succeeded.'
		COMMIT TRANSACTION
		INSERT INTO [dbo].[DBVersion] ([DBVersionNumber], [UpdateDate]) VALUES (11, GETDATE())
	END
		ELSE PRINT 'The database update failed.'
	DROP TABLE #tmpErrors11
END